package cn.stylefeng.guns.modular.forecastLoadInformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.forecastLoadInformation.entity.QyuanPowerSdGridForecast;
import cn.stylefeng.guns.modular.forecastLoadInformation.enums.QyuanPowerSdGridForecastExceptionEnum;
import cn.stylefeng.guns.modular.forecastLoadInformation.mapper.QyuanPowerSdGridForecastMapper;
import cn.stylefeng.guns.modular.forecastLoadInformation.pojo.request.QyuanPowerSdGridForecastRequest;
import cn.stylefeng.guns.modular.forecastLoadInformation.service.QyuanPowerSdGridForecastService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 负荷信息预测信息表，记录每天96个时间点的电网运行预测数据业务实现层
 *
 * @author 我是谭先生🐻
 * @date 2025/03/14 10:41
 */
@Service
public class QyuanPowerSdGridForecastServiceImpl extends ServiceImpl<QyuanPowerSdGridForecastMapper, QyuanPowerSdGridForecast> implements QyuanPowerSdGridForecastService {

	@Override
    public void add(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        QyuanPowerSdGridForecast qyuanPowerSdGridForecast = new QyuanPowerSdGridForecast();
        BeanUtil.copyProperties(qyuanPowerSdGridForecastRequest, qyuanPowerSdGridForecast);
        this.save(qyuanPowerSdGridForecast);
    }

    @Override
    public void del(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        QyuanPowerSdGridForecast qyuanPowerSdGridForecast = this.queryQyuanPowerSdGridForecast(qyuanPowerSdGridForecastRequest);
        this.removeById(qyuanPowerSdGridForecast.getId());
    }

    @Override
    public void edit(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        QyuanPowerSdGridForecast qyuanPowerSdGridForecast = this.queryQyuanPowerSdGridForecast(qyuanPowerSdGridForecastRequest);
        BeanUtil.copyProperties(qyuanPowerSdGridForecastRequest, qyuanPowerSdGridForecast);
        this.updateById(qyuanPowerSdGridForecast);
    }

    @Override
    public QyuanPowerSdGridForecast detail(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        return this.queryQyuanPowerSdGridForecast(qyuanPowerSdGridForecastRequest);
    }

    @Override
    public PageResult<QyuanPowerSdGridForecast> findPage(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        LambdaQueryWrapper<QyuanPowerSdGridForecast> wrapper = createWrapper(qyuanPowerSdGridForecastRequest);
        Page<QyuanPowerSdGridForecast> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<QyuanPowerSdGridForecast> findList(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        LambdaQueryWrapper<QyuanPowerSdGridForecast> wrapper = this.createWrapper(qyuanPowerSdGridForecastRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 10:41
     */
    private QyuanPowerSdGridForecast queryQyuanPowerSdGridForecast(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        QyuanPowerSdGridForecast qyuanPowerSdGridForecast = this.getById(qyuanPowerSdGridForecastRequest.getId());
        if (ObjectUtil.isEmpty(qyuanPowerSdGridForecast)) {
            throw new ServiceException(QyuanPowerSdGridForecastExceptionEnum.QYUAN_POWER_SD_GRID_FORECAST_NOT_EXISTED);
        }
        return qyuanPowerSdGridForecast;
    }

    /**
     * 创建查询wrapper
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 10:41
     */
    private LambdaQueryWrapper<QyuanPowerSdGridForecast> createWrapper(QyuanPowerSdGridForecastRequest qyuanPowerSdGridForecastRequest) {
        LambdaQueryWrapper<QyuanPowerSdGridForecast> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = qyuanPowerSdGridForecastRequest.getId();
        String usageDate = qyuanPowerSdGridForecastRequest.getUsageDate();
        Integer timePoint = qyuanPowerSdGridForecastRequest.getTimePoint();
        String timeSlot = qyuanPowerSdGridForecastRequest.getTimeSlot();
        BigDecimal powerDirectLoad = qyuanPowerSdGridForecastRequest.getPowerDirectLoad();
        BigDecimal powerLocalGeneration = qyuanPowerSdGridForecastRequest.getPowerLocalGeneration();
        BigDecimal powerInterconnectionLoad = qyuanPowerSdGridForecastRequest.getPowerInterconnectionLoad();
        BigDecimal powerWindGeneration = qyuanPowerSdGridForecastRequest.getPowerWindGeneration();
        BigDecimal powerSolarGeneration = qyuanPowerSdGridForecastRequest.getPowerSolarGeneration();
        BigDecimal powerNuclearGeneration = qyuanPowerSdGridForecastRequest.getPowerNuclearGeneration();
        BigDecimal powerSelfGeneration = qyuanPowerSdGridForecastRequest.getPowerSelfGeneration();
        BigDecimal powerExperimentalGeneration = qyuanPowerSdGridForecastRequest.getPowerExperimentalGeneration();
        String createdAt = qyuanPowerSdGridForecastRequest.getCreateTime();
        String updatedAt = qyuanPowerSdGridForecastRequest.getUpdateTime();

        queryWrapper.eq(ObjectUtil.isNotNull(id), QyuanPowerSdGridForecast::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(usageDate), QyuanPowerSdGridForecast::getUsageDate, usageDate);
        queryWrapper.eq(ObjectUtil.isNotNull(timePoint), QyuanPowerSdGridForecast::getTimePoint, timePoint);
        queryWrapper.like(ObjectUtil.isNotEmpty(timeSlot), QyuanPowerSdGridForecast::getTimeSlot, timeSlot);
        queryWrapper.eq(ObjectUtil.isNotNull(powerDirectLoad), QyuanPowerSdGridForecast::getPowerDirectLoad, powerDirectLoad);
        queryWrapper.eq(ObjectUtil.isNotNull(powerLocalGeneration), QyuanPowerSdGridForecast::getPowerLocalGeneration, powerLocalGeneration);
        queryWrapper.eq(ObjectUtil.isNotNull(powerInterconnectionLoad), QyuanPowerSdGridForecast::getPowerInterconnectionLoad, powerInterconnectionLoad);
        queryWrapper.eq(ObjectUtil.isNotNull(powerWindGeneration), QyuanPowerSdGridForecast::getPowerWindGeneration, powerWindGeneration);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSolarGeneration), QyuanPowerSdGridForecast::getPowerSolarGeneration, powerSolarGeneration);
        queryWrapper.eq(ObjectUtil.isNotNull(powerNuclearGeneration), QyuanPowerSdGridForecast::getPowerNuclearGeneration, powerNuclearGeneration);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSelfGeneration), QyuanPowerSdGridForecast::getPowerSelfGeneration, powerSelfGeneration);
        queryWrapper.eq(ObjectUtil.isNotNull(powerExperimentalGeneration), QyuanPowerSdGridForecast::getPowerExperimentalGeneration, powerExperimentalGeneration);
        queryWrapper.eq(ObjectUtil.isNotNull(createdAt), QyuanPowerSdGridForecast::getCreateTime, createdAt);
        queryWrapper.eq(ObjectUtil.isNotNull(updatedAt), QyuanPowerSdGridForecast::getUpdateTime, updatedAt);

        return queryWrapper;
    }

}